package com.donleo.oauth2.gateway.fallback;

import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
 * 网关熔断降级处理类
 *
 * @author liangd
 * @since 2021-03-11 18:04
 */
@Component
public class ServerProviderFallback implements FallbackProvider {
    /**
     * getRoute方法的返回值就是要监听的挂掉的微服务的名字
     * 如果需要所有服务都走这个熔断回退，则return "*" 或 return null
     */
    @Override
    public String getRoute() {
        return "*";
    }

    /**
     * 当服务无法执行的时候，返回托底信息
     */
    @Override
    public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
        return new ClientHttpResponse() {
            /**
             *  ClientHttpResponse的fallback的状态码
             */
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return HttpStatus.OK;
            }

            /**
             * ClientHttpResponse的fallback的状态码
             */
            @Override
            public int getRawStatusCode() throws IOException {
                return this.getStatusCode().value();
            }

            /**
             * ClientHttpResponse的fallback的状态码
             */
            @Override
            public String getStatusText() throws IOException {
                return this.getStatusCode().getReasonPhrase();
            }

            @Override
            public void close() {

            }

            /**
             * 设置响应体信息
             */
            @Override
            public InputStream getBody() throws IOException {
                String content = "服务不可用，请与管理员联系";
                return new ByteArrayInputStream(content.getBytes());
            }

            /**
             * 设置响应头信息
             */
            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders headers = new HttpHeaders();
                MediaType mt = new MediaType("application", "json", Charset.forName("utf-8"));
                headers.setContentType(mt);
                return headers;
            }
        };
    }
}
